package com.qcby.service.impl;

import com.qcby.dao.UserDao;
import com.qcby.entity.LoginUser;
import com.qcby.entity.Result;
import com.qcby.entity.User;
import com.qcby.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public Result login(LoginUser user, HttpServletRequest request) {
        LoginUser reurnuser = userDao.login(user);
        System.out.println(reurnuser);
        if (reurnuser != null) {
            HttpSession session = request.getSession();
            session.setAttribute("user", reurnuser);
            session.setAttribute("id", reurnuser.getId());
            session.setAttribute("username", reurnuser.getUsername());
            if(user.getRole() == 0){
                return Result.success(reurnuser, 200);
            }else{
                return Result.success(reurnuser,300);
            }

        } else {
            return Result.error("用户名或密码错误");
        }
    }
    @Override
    public Result findStudentById(Integer id) {
        User user = userDao.findStudentById(id);
        return Result.success(user);
    }

    @Override
    public Result findTeacherById(Integer id) {
        User user = userDao.findTeacherById(id);
        return Result.success(user);
    }

}
